using System;
using System.ComponentModel;

namespace GeneralDFS.BusinessLogical.RemoteObject
{
    public partial class NameNodeRemoteObject
    {
        private BackgroundWorker m_StabilizeSuccessors = new BackgroundWorker();
        private BackgroundWorker m_StabilizePredecessors = new BackgroundWorker();
        private BackgroundWorker m_UpdateFingerTable = new BackgroundWorker();
        private BackgroundWorker m_Rejoin = new BackgroundWorker();

        /// <summary>
        /// Start the maintenance tasks to run as background worker threads.
        /// </summary>
        private void StartMaintenance()
        {
            m_StabilizeSuccessors.DoWork += new DoWorkEventHandler(this.StabilizeSuccessors);
            m_StabilizeSuccessors.WorkerSupportsCancellation = true;
            m_StabilizeSuccessors.RunWorkerAsync();

            m_StabilizePredecessors.DoWork += new DoWorkEventHandler(this.StabilizePredecessors);
            m_StabilizePredecessors.WorkerSupportsCancellation = true;
            m_StabilizePredecessors.RunWorkerAsync();

            m_UpdateFingerTable.DoWork += new DoWorkEventHandler(this.UpdateFingerTable);
            m_UpdateFingerTable.WorkerSupportsCancellation = true;
            m_UpdateFingerTable.RunWorkerAsync();

            m_Rejoin.DoWork += new DoWorkEventHandler(this.ReJoin);
            m_Rejoin.WorkerSupportsCancellation = true;
            m_Rejoin.RunWorkerAsync();
        }

        /// <summary>
        /// Stop the maintenance tasks (asynchronously) that are currently running.
        /// </summary>
        private void StopMaintenance()
        {
            m_StabilizeSuccessors.CancelAsync();
            m_StabilizePredecessors.CancelAsync();
            m_UpdateFingerTable.CancelAsync();
            m_Rejoin.CancelAsync();
        }
    }
}
